package action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

import dao.FriendshipDAO;
import dao.UserDAO;

import bean.Friendship;
import bean.FriendshipId;
import bean.User;

public class FriendAction extends DispatchAction {

	public ActionForward browse(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		SimpleDateFormat format =
            new SimpleDateFormat("MMM dd yyyy");
		
		User user=(User)request.getSession(true).getAttribute("user");
		int userid=user.getUId();
		
		FriendshipDAO fDAO=new FriendshipDAO(); 
		UserDAO userDAO=new UserDAO();
		user=userDAO.findById(userid);
		
		Set<Friendship> friends=user.getFriendshipsForUId();
		Iterator<Friendship> i=friends.iterator();
		Friendship temp=null;
		
		StringBuffer responseXML=new StringBuffer();
		responseXML.append("<friends>\n");
		while(i.hasNext()){
			temp=i.next();
			
			responseXML.append("<friend>\n");
			responseXML.append("<friendId>");
			responseXML.append(temp.getId().getUser_1().getUId());
			responseXML.append("</friendId>\n");
			responseXML.append("<friendName>");
			responseXML.append( userDAO.findById( temp.getId().getUser_1().getUId()).getUserfname()+" "+userDAO.findById( temp.getId().getUser_1().getUId()).getUserlname());
			responseXML.append("</friendName>\n");
			
			responseXML.append("<createdAt>");
			responseXML.append(format.format( temp.getCreatedAt()));
			responseXML.append("</createdAt>\n");
			responseXML.append("</friend>\n");	
		}		
		responseXML.append("</friends>");
		System.out.println(responseXML.toString());
		response.setContentType("text/xml");
		try {
			ServletOutputStream out=response.getOutputStream();
			out.print(responseXML.toString());			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ActionForward delete(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		
		User user=(User)request.getSession(true).getAttribute("user");
		UserDAO userDAO=new UserDAO();
		FriendshipDAO fDAO=new FriendshipDAO();
		int friendId=Integer.parseInt( request.getParameter("fid"));
		
		User friend=userDAO.findById(friendId);
		FriendshipId fId=new FriendshipId(user, friend);
		
		fDAO.delete(fDAO.findById(fId));
		
		return mapping.findForward("success");
	}
	
	public ActionForward add(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		
		User oldUser=(User)request.getSession(true).getAttribute("user");
		int friendId= Integer.parseInt( request.getParameter("friendId"));
		UserDAO userDAO=new UserDAO();
		FriendshipDAO fDAO=new FriendshipDAO();
		
		User user=userDAO.findById(oldUser.getUId());
		User friend=userDAO.findById(friendId);
		
		FriendshipId fId=new FriendshipId(user, friend);
		Friendship f=new Friendship(fId, new Date());
		fDAO.save(f);
		
		return mapping.findForward("success");
	}	
	
	public ActionForward browseall(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		SimpleDateFormat format =
            new SimpleDateFormat("MMM dd yyyy");
		
		User user=(User)request.getSession(true).getAttribute("user");
		int userid=user.getUId();
		
		UserDAO userDAO=new UserDAO();
		FriendshipDAO fDAO=new FriendshipDAO();
		user=userDAO.findById(userid);
		
		List<User> persistusers=userDAO.findAll();
		List<User> users=new ArrayList<User>();
		Set<Friendship> friends=user.getFriendshipsForUId();
		Friendship temp=null;		
		
		int existedId=0;
		FriendshipId fId=null;
		for(User u:persistusers){
			if(!u.getUId().equals(user.getUId())){
				fId=new FriendshipId(userDAO.findById(user.getUId()), userDAO.findById( u.getUId()));
				if(!friends.contains(fDAO.findById(fId))){
					User tempU=new User(u.getUId(), u.getUserfname(), u.getUserlname(),
							u.getGender(), u.getDob(), u.getEmail(), u.getPassword());
					users.add(tempU);
				}
			}
		}
		
		StringBuffer responseXML=new StringBuffer();
		responseXML.append("<users>\n");
		for(User u:users){
			responseXML.append("<user>\n");
			responseXML.append("<userId>");
			responseXML.append(u.getUId());
			responseXML.append("</userId>\n");
			responseXML.append("<userName>");
			responseXML.append( u.getUserfname()+" "+u.getUserlname());
			responseXML.append("</userName>\n");
			
			responseXML.append("<location>");
			responseXML.append( u.getUserlocation());
			responseXML.append("</location>\n");
			responseXML.append("</user>\n");
		}
		responseXML.append("</users>");
		System.out.println(responseXML.toString());
		response.setContentType("text/xml");
		try {
			ServletOutputStream out=response.getOutputStream();
			out.print(responseXML.toString());			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
